package com.budwk.app.sms.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sms.dto.SmsMsgDTO;
import com.budwk.app.sms.service.TenYuSmsService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ZY.Wang
 * @since 2024/3/15
 */
@IocBean
@Slf4j
public class TenYuSmsServiceImpl implements TenYuSmsService {

    @Inject
    private PropertiesProxy propertiesProxy;

    public Integer tenYuSendMessageBatch(SmsMsgDTO msgDTO) {
        try {
            String enable = propertiesProxy.get("opa.sms.enable");
            if (enable.equals("false")) return 0;

            String userName = "xyxz";

            String content = msgDTO.getTemplateParamList().get(0);
            if (StringUtils.isBlank(content)) return 0;
            content = "【祥云小助】" + content;

            String timeStamp = (new Date().getTime()) + "";
            List<String> phoneList = Lists.newArrayList(msgDTO.getPhoneNumberSet());

            String md5Password = DigestUtils.md5Hex("Ln8zRbp0");
            String sign = DigestUtils.md5Hex(userName + timeStamp + md5Password);

            Map<String, Object> paramMap = Maps.newHashMap();
            paramMap.put("userName", userName);
            paramMap.put("content", content);
            paramMap.put("phoneList", phoneList);
            paramMap.put("timeStamp", timeStamp);
            paramMap.put("sign", sign);
            String requestBodyStr = JSONObject.toJSONString(paramMap);
            String resStr = HttpUtil.post("https://v3.it1688.com.cn:7000/sms/sign/sendList", requestBodyStr);

            JSONObject resJson = JSONObject.parseObject(resStr);
            Integer code = resJson.getInteger("code");

            if (code == 0) return 1;
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("tenYu sms send error! please check this error!");
        }
        return 0;
    }

    public Integer tenYuSendMessage(SmsMsgDTO msgDTO) {
        try {
            String enable = propertiesProxy.get("opa.sms.enable");
            if (enable.equals("false")) return 0;

            String userName = "xyxz";

            List<Map<String, String>> messageList = Lists.newArrayList();
            String content = msgDTO.getTemplateParamList().get(0);
            if (StringUtils.isBlank(content)) return 0;
            content = "【祥云小助】" + content;
            for (String phone : msgDTO.getPhoneNumberSet()) {
                Map<String, String> messageMap = Maps.newHashMap();
                messageMap.put("phone", phone);
                messageMap.put("content", content);
                messageList.add(messageMap);
            }

            String timeStamp = (new Date().getTime()) + "";

            String md5Password = DigestUtils.md5Hex("Ln8zRbp0");
            String sign = DigestUtils.md5Hex(userName + timeStamp + md5Password);

            Map<String, Object> paramMap = Maps.newHashMap();
            paramMap.put("userName", userName);
            paramMap.put("messageList", messageList);
            paramMap.put("timeStamp", timeStamp);
            paramMap.put("sign", sign);
            String requestBodyStr = JSONObject.toJSONString(paramMap);
            String resStr = HttpUtil.post("https://v3.it1688.com.cn:7000/sms/sign/send", requestBodyStr);

            JSONObject resJson = JSONObject.parseObject(resStr);
            Integer code = resJson.getInteger("code");

            if (code == 0) return 1;
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("tenYu sms send error! please check this error!");
        }

        return 0;
    }
}
